package fr.android.ui;

import to52.android.screens.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import fr.android.core.SimpleGestureFilter;


public abstract class SliceNavigator extends Activity implements SimpleGestureFilter.SimpleGestureListener {

	private SimpleGestureFilter detector;
	
	private Button btnPrevious;
	
	private Button btnNext;
	
	private TextView navigatorTitle;
	
	private boolean slideLeftEnabled = true;
	
	private boolean slideRightEnabled = true;
	
	
	/**
	 * For children
	 */
	public abstract void afterCreate();
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.navigationlayout);	
		
		this.detector = new SimpleGestureFilter(this, this);
		
		navigatorTitle = (TextView) findViewById(R.id.navigation_item_title);
		
        btnPrevious = (Button) findViewById(R.id.navigation_btn_previous);
        btnPrevious.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
            	onSliceLeft();
			}
        });
	
        btnNext = (Button) findViewById(R.id.navigation_btn_next);
        btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
            	onSliceRight();
			}
        });
        
		this.afterCreate();
	}

	
	@Override
	public boolean dispatchTouchEvent(MotionEvent me) {
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

	@Override
	public void onSwipe(int direction) {

		switch (direction) {

		case SimpleGestureFilter.SWIPE_LEFT:
			if ( slideRightEnabled ) {
				this.onSliceRight();
			}
			break;
		case SimpleGestureFilter.SWIPE_RIGHT:
			if ( slideLeftEnabled ) {
				this.onSliceLeft();
			}
			break;
		case SimpleGestureFilter.SWIPE_DOWN:
			break;
		case SimpleGestureFilter.SWIPE_UP:
			break;

		}
	}

	
	@Override
	public void onDoubleTap() {
		//Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
	}
	
	
	public abstract void onSliceLeft();
	
	
	public abstract void onSliceRight();
	
	
	protected void setTitle(String title) {
		navigatorTitle.setText(title);
	}
	
	protected void setSliceLeftEnable(boolean enable) {
		slideLeftEnabled = enable;
		btnPrevious.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
	}
	
	protected void setSliceRightEnable(boolean enable) {
		slideRightEnabled = enable;
		btnNext.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
	}

	protected void playLeftAnimation() {
		LinearLayout ll = (LinearLayout)findViewById(R.id.animated_slice_layout);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left);
        ll.startAnimation(animation);
	}
	
	protected void playRightAnimation() {
		LinearLayout ll = (LinearLayout)findViewById(R.id.animated_slice_layout);
		Animation animation  = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.our_slide_in_right);
        ll.startAnimation(animation);
	}
	
}
